﻿$(function ()
{
	$('ul').each(function ()
	{
		if ($(this).children('li').length === 1)
		{
			$(this).children('li.emptyMessage').show();
		}
	});
	$("#sortable1").sortable({
		connectWith: ".connectedSortable",
		items: "li:not(.unsortable)",
		placeholder: "ghost-highlight",
		revert: 200,
		tolerance: "intersect",
		over: function (event, ui)
		{
			if ($(ui.item).hasClass('manager-selection'))
			{
				$('div.applications').children('ul')
                    .children('li.ghost-highlight')
                    .removeClass('ghost-highlight')
                    .attr('style', 'height:0px; padding:0px; border: none;');
				$('div.available-apps').children('div').children('div.applications').addClass('warning-background');
				$('div.warning').show();
			}
			else
			{
				if ($('.positions').children('div.ghost-box'))
				{
					$('.positions').children('div.ghost-box').fadeOut("normal", function ()
					{
						$(this).remove();
					});
				}
			}
		},
		out: function (event, ui)
		{
			if ($(ui.item).hasClass('manager-selection'))
			{
				$('div.applications').removeClass('warning-background');
				$('div.warning').hide();
			}
		},
		receive: function (event, ui)
		{
			$(ui.item).removeAttr('style');
			if ($(ui.item).hasClass('manager-selection'))
			{
				$(ui.sender).sortable('cancel');
			}
			else
			{
				var nb = $('.positions').children('div').length - 1;
				$('.positions').children('div:last').fadeOut("normal", function ()
				{
					$(this).remove();
				});
			}

			//show empty message on sender if applicable
			$('li.emptyMessage', this).hide();
			if ($('li:not(.emptyMessage)', ui.sender).length == 0)
			{
				$('li.emptyMessage', ui.sender).show();
			}
			else
			{
				$('li.emptyMessage', ui.sender).hide();
			}
		},
		start: function (event, ui)
		{
			$(ui.item).removeClass('grab');
			$(ui.item).addClass('grabbing');
		},
		stop: function (event, ui)
		{
			$(ui.item).removeClass('grabbing');
			$(ui.item).addClass('grab');
		}
	}).disableSelection();

	$("#sortable2").sortable({
		connectWith: [".connectedSortable", ".connectedToTrash"],
		dropOnEmpty: true,
		placeholder: "ghost-highlight",
		items: "li:not(.unsortable)",
		revert: 200,
		tolerance: "intersect",
		receive: function (event, ui)
		{
			$(ui.item).removeAttr('style').addClass('manager-selection');
			if (isManager !== null)
			{
				if (!$(ui.item).hasClass('manager'))
				{
					$(ui.item).addClass('manager')
				}
			}
			else
			{
				if (!$(ui.item).hasClass('perso'))
				{
					$(ui.item).addClass('perso')
				}
			}
			$('.positions').children('div:last').remove();
			var nb = $('.positions').children('div').length + 1;
			$('<div><span>' + nb + '</span></div>').appendTo('.positions');

			//show empty message on sender if applicable
			$('li.emptyMessage', this).hide();
			if ($('li:not(.emptyMessage)', ui.sender).length === 0)
			{
				$('li.emptyMessage', ui.sender).show();
			}
			else
			{
				$('li.emptyMessage', ui.sender).hide();
			}
		},
		start: function (event, ui)
		{
			/*$('.unsortable', this).each(function (){
			var $this = $(this);
			$this.data('pos', $this.index());
			});*/
			$(ui.item).removeClass('grab');
			$(ui.item).addClass('grabbing');
		},

		stop: function (event, ui)
		{
			$(ui.item).removeClass('grabbing');
			$(ui.item).addClass('grab');
		},

		/*		change: function (){
		$sortable = $(this);
		$statics = $('.unsortable', this).detach();
		$helper = $('<li></li>').prependTo($sortable);
		$statics.each(function (){
		var $this = $(this);
		var target = $this.data('pos');
		$this.insertAfter($('li', $sortable).eq(target));
		});
		$helper.remove();
		},
		*/
		over: function (event, ui)
		{
			if (!$(ui.item).hasClass('manager-selection'))
			{
				var nb = $('.positions').children('div').length + 1;
				$('<div class="ghost-box" style="opacity:0.7; border:1px dashed;"><span>' + nb + '</span></div>').appendTo('.positions');
			}
		},
		out: function (event, ui)
		{
			if ($('.positions').children('div:last').hasClass('ghost-box')
			&& $('ul#sortable2').children('li.ghost-highlight').length === 0)
			{
				$('.positions').children('div:last').remove();
			}
		}
	}).disableSelection();
	$("#sortable3").sortable({
		connectWith: ".connectedToTrash",
		dropOnEmpty: true,
		placeholder: "ghost-highlight",
		items: "li:not(.unsortable)",
		revert: 200,
		tolerance: "intersect",
		receive: function (event, ui)
		{
			$(ui.item).removeAttr('style').removeClass('manager-selection');
			$(ui.item).children('div.oblig').remove();
			var nb = $('.positions').children('div').length - 1;
			$('.positions').children('div:last').fadeOut("normal", function ()
			{
				$(this).remove();
			});
			//hide empty message on receiver
			$('li.emptyMessage', this).hide();
			//show empty message on sender if applicable
			if ($('li:not(.emptyMessage)', ui.sender).length == 0)
			{
				$('li.emptyMessage', ui.sender).show();
			}
			else
			{
				$('li.emptyMessage', ui.sender).hide();
			}
		},
		start: function (event, ui)
		{
			$(ui.item).removeClass('grab');
			$(ui.item).addClass('grabbing');
		},
		stop: function (event, ui)
		{
			$(ui.item).removeClass('grabbing');
			$(ui.item).addClass('grab');
		}
	}).disableSelection();
});